package main

import (
	"fmt"
	"spider/common"
	"spider/itemSaver"
	"spider/models"
	"spider/models/meituan/meituanMeishiPc"
)

func main() {
	err := common.InitConfig("./conf.json")
	if err != nil {
		panic(err)
	}
	models.Connect()
	var lists []meituanMeishiPc.TableMeiTuanLicense
	orm := models.G_ormdb
	id := 0

	for {
		res := orm.Table("meituan_license").Where("id>?", id).Limit(20).Find(&lists).Order("id asc")
		if res.Error != nil {
			panic(res.Error)
		}
		fmt.Println("id===", id)
		num := len(lists)
		if num < 1 {
			break
		}
		id = lists[num-1].Id

		for _, item := range lists {
			poiId := item.PoiId
			BusinessLicenceImgUrl := item.BusinessLicenceImgUrl
			RestaurantLicenceImgUrl := item.RestaurantLicenceImgUrl
			path0, err := common.BuildPathFile(poiId, 0)

			oss_path0 := fmt.Sprintf("test/meituan_licence/%d/%d.jpg", poiId, 0);
			ossUrl0 := fmt.Sprintf("https://img2.lepucdn.cn/test/meituan_licence/%d/%d.jpg@!original.jpg", poiId, 0);
			if err != nil || len(BusinessLicenceImgUrl) < 1 {
				fmt.Printf("poiId=%d,err=%s\n", poiId, err)
			} else {
				if common.Exists(path0) {

					err = itemSaver.OssUpload(path0, oss_path0);
					if err != nil {
						fmt.Println(err)
					} else {
						orm.Model(&meituanMeishiPc.TableMeiTuanLicense{}).Where("poiId=?", poiId).
							Update("ossBusinessLicenceImgUrl", ossUrl0)
						if orm.Error != nil {
							fmt.Println(orm.Error)
						}
					}
				}
			}

			path1, err := common.BuildPathFile(poiId, 1)
			oss_path1 := fmt.Sprintf("test/meituan_licence/%d/%d.jpg", poiId, 1);
			ossUrl1 := fmt.Sprintf("https://img2.lepucdn.cn/test/meituan_licence/%d/%d.jpg@!original.jpg", poiId, 1);
			if err != nil || len(RestaurantLicenceImgUrl) < 1 {
				fmt.Printf("poiId=%d,err=%s\n", poiId, err)
				continue;
			}
			if common.Exists(path1) {
				err = itemSaver.OssUpload(path1, oss_path1);
				if err != nil {
					fmt.Println(err)
				} else {
					orm.Model(&meituanMeishiPc.TableMeiTuanLicense{}).Where("poiId=?", poiId).
						Update("ossRestaurantLicenceImgUrl", ossUrl1);
					if orm.Error != nil {
						fmt.Println(orm.Error)
					}
				}
			}

		}

	}

}
